package com.mall.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.mall.common.domain.R;
import com.mall.user.entity.DO.Member;
import com.mall.user.mapper.MemberMapper;
import com.mall.user.service.MemberService;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

import static cn.hutool.core.date.DateUtil.now;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Autowired
    private MemberMapper memberMapper;
    @Override
    public R regist(Member member) {
        //判断username是否存在
        Map map  = new HashMap();
        map.put("username",member.getUsername());
        List<Member> m = memberMapper.selectByMap(map);
        if (m.size() > 0){
            return R.error("用户名已存在");
        }else {
            //插入createtime属性
            member.setCreateTime(now());
            int insert = memberMapper.insert(member);
            if (insert > 0){
                return R.ok("注册成功");
            }else {
                return R.error("注册失败");
            }
        }
    }

    @Override
    public R queryAll(Page page) {
        Page res = memberMapper.selectPage(page, null);
        if (res != null){
            return R.ok(res);
        }else {
            return R.error("查询失败");
        }
    }

    @Override
    public R login(Member member) {
        return null;
    }
    public int text(String phone){
            //生产环境请求地址：app.cloopen.com
            String serverIp = "app.cloopen.com";
            //请求端口
            String serverPort = "8883";
            //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
            String accountSId = "2c94811c946f6bfb019638b99ef25228";
            String accountToken = "6a7c005662e74b94aa26b073a3f0a734";
            //请使用管理控制台中已创建应用的APPID
            String appId = "2c94811c946f6bfb019638b9a091522f";
            CCPRestSmsSDK sdk = new CCPRestSmsSDK();
            sdk.init(serverIp, serverPort);
            sdk.setAccount(accountSId, accountToken);
            sdk.setAppId(appId);
            sdk.setBodyType(BodyType.Type_JSON);
            String to = phone;  // 仅可以测试官网中填入的测试号码
            String templateId= "1";
            // 测试开发支持的文案如下:【云通讯】您的验证码是{1}，请于{2}分钟内正确输入。其中{1}和{2}为短信模板参数。
            // 随机一个验证码验证码
            int code = ThreadLocalRandom.current().nextInt(1000, 10000); // 1000~9999 //RandomUtils.nextInt(1000, 9999);
            String[] datas = {String.valueOf(code), "1"};
            String subAppend="1234";  //可选 扩展码，四位数字 0~9999
            String reqId= UUID.randomUUID().toString();  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
            //HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
            HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas,subAppend,reqId);
            if("000000".equals(result.get("statusCode"))){
                //正常返回输出data包体信息（map）
                HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
                Set<String> keySet = data.keySet();
                for(String key:keySet){
                    Object object = data.get(key);
                    System.out.println(key +" = "+object);
                    System.out.println("手机号: " + to + ", 验证码: " + code);
                }
            }else{
                //异常返回输出错误码和错误信息
                System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            }
            return code;
    }
}
